package com.ruoyi.system.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dto.PreventionCheckTaskConfigDTO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskConfigVO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskCountVO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskVO;
import com.ruoyi.system.entity.PreventionCheckTask;
import com.ruoyi.system.entity.PreventionCheckTaskConfig;
import com.ruoyi.system.entity.SysActing;
import com.ruoyi.system.entity.SysDutyUser;
import com.ruoyi.system.mapper.PreventionCheckTaskConfigMapper;
import com.ruoyi.system.mapper.PreventionCheckTaskMapper;
import com.ruoyi.system.service.PreventionCheckTaskService;
import com.ruoyi.system.service.SysActingService;
import com.ruoyi.system.service.SysDutyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 排查任务表(PreventionCheckTask)表服务实现类
 *
 * @author makejava
 * @since 2022-11-18 14:04:46
 */
@Service("preventionCheckTaskService")
public class PreventionCheckTaskServiceImpl implements PreventionCheckTaskService {

    @Resource
    private PreventionCheckTaskMapper preventionCheckTaskDao;

    @Autowired
    private PreventionCheckTaskConfigMapper preventionCheckTaskConfigMapper;

    @Resource
    private SysActingService sysActingService;

    @Resource
    private SysDutyUserService sysDutyUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PreventionCheckTask queryById(Integer id) {
        return this.preventionCheckTaskDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param preventionCheckTask 筛选条件
     * @return 查询结果
     */
    @Override
    public List<PreventionCheckTask> queryByPage(PreventionCheckTask preventionCheckTask) {
        if (SecurityUtils.getUserId() != 1) {
            String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
            preventionCheckTask.setCheckUserName(nickName);
        }
        List<PreventionCheckTask> preventionCheckTasks = preventionCheckTaskDao.queryAllByLimit(preventionCheckTask);
        for (int i = 0; i < preventionCheckTasks.size(); i++) {
            PreventionCheckTask checkTask = preventionCheckTasks.get(i);
            Integer checkTaskConfigId = checkTask.getCheckTaskConfigId();
            PreventionCheckTaskConfigVO preventionCheckTaskConfigVO = new PreventionCheckTaskConfigVO();
            preventionCheckTaskConfigVO.setId(checkTaskConfigId);
            List<PreventionCheckTaskConfigVO> preventionCheckTaskConfigVOS = preventionCheckTaskConfigMapper.queryAllByLimit(preventionCheckTaskConfigVO);
            checkTask.setPreventionCheckTaskConfig(preventionCheckTaskConfigVOS.get(0));
        }
        return preventionCheckTasks;
    }


    /**
     * 新增数据
     *
     * @param preventionCheckTask 实例对象
     * @return 实例对象
     */
    @Override
    public PreventionCheckTask insert(PreventionCheckTask preventionCheckTask) {
        this.preventionCheckTaskDao.insert(preventionCheckTask);
        return preventionCheckTask;
    }

    /**
     * 修改数据
     *
     * @param preventionCheckTask 实例对象
     * @return 实例对象
     */
    @Override
    public PreventionCheckTask update(PreventionCheckTask preventionCheckTask) {
        this.preventionCheckTaskDao.update(preventionCheckTask);
        return this.queryById(preventionCheckTask.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.preventionCheckTaskDao.deleteById(id) > 0;
    }

    @Override
    public PreventionCheckTask queryByConfigId(Integer id) {
        return preventionCheckTaskDao.queryByConfigId(id);
    }

    @Override
    public PreventionCheckTaskVO getTaskInfoById(Integer taskId) {
        return preventionCheckTaskDao.getTaskInfoById(taskId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void  updateTaskState(PreventionCheckTask preventionCheckTask) {
        this.preventionCheckTaskDao.update(preventionCheckTask);
        // 更新待办任务的状态
        sysActingService.completeActing("隐患","排查任务",preventionCheckTask.getId());
    }

    @Override
    public List<PreventionCheckTaskVO> queryByUserName(PreventionCheckTaskConfigDTO preventionCheckTaskConfigDTO) {
        return preventionCheckTaskDao.queryByUserName(preventionCheckTaskConfigDTO);
    }

    @Override
    public  List<PreventionCheckTaskCountVO> getTaskDayTemp(String dayTo) {
        return preventionCheckTaskDao.getTaskDayTemp(dayTo);
    }

    @Override
    public  List<PreventionCheckTaskCountVO> getTaskMeekTemp(String weekStart,String weekEnd) {
        return preventionCheckTaskDao.getTaskMeekTemp(weekStart,weekEnd);
    }

    @Override
    public  List<PreventionCheckTaskCountVO> getTaskMonthTemp(String monthTo) {
        return preventionCheckTaskDao.getTaskMonthTemp(monthTo);
    }

    @Override
    public List<PreventionCheckTaskCountVO> getallTask() {
        return preventionCheckTaskDao.getallTask();
    }

    @Override
    public Map<String,List<PreventionCheckTaskVO>> getTaskInfoByDuty(String startTime, String endTime) {
        Map<String,List<PreventionCheckTaskVO>> map = new HashMap<>();
        // 季度
        SysDutyUser j = sysDutyUserService.getByDutyType("技术负责人");
        String jUserName = j.getUserName();
        List<PreventionCheckTaskVO> jlist = preventionCheckTaskDao.queryByDuty(jUserName,startTime,endTime);
        map.put("j",jlist);
        // 周
        SysDutyUser c = sysDutyUserService.getByDutyType("操作负责人");
        String cUserName = c.getUserName();
        List<PreventionCheckTaskVO> clist = preventionCheckTaskDao.queryByDuty(cUserName,startTime,endTime);
        map.put("c",clist);
        return map;
    }

    @Override
    public List<PreventionCheckTask> queryByPageApp(PreventionCheckTask preventionCheckTask) {
        if (SecurityUtils.getUserId() != 1) {
            String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
            preventionCheckTask.setCheckUserName(nickName);
        }
        List<PreventionCheckTask> preventionCheckTasks = preventionCheckTaskDao.queryAllByLimitApp(preventionCheckTask);
        if (CollUtil.isEmpty(preventionCheckTasks)) {
            return preventionCheckTasks;
        }
        for (int i = 0; i < preventionCheckTasks.size(); i++) {
            PreventionCheckTask checkTask = preventionCheckTasks.get(i);
            Integer checkTaskConfigId = checkTask.getCheckTaskConfigId();
            PreventionCheckTaskConfigVO preventionCheckTaskConfigVO = new PreventionCheckTaskConfigVO();
            preventionCheckTaskConfigVO.setId(checkTaskConfigId);
            List<PreventionCheckTaskConfigVO> preventionCheckTaskConfigVOS = preventionCheckTaskConfigMapper.queryAllByLimit(preventionCheckTaskConfigVO);
            if (StringUtils.isEmpty(preventionCheckTaskConfigVOS)) {
                continue;
            }
            checkTask.setPreventionCheckTaskConfig(preventionCheckTaskConfigVOS.get(0));
        }
        return preventionCheckTasks;
    }
}
